package com.ruoyi.web.controller.job;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.YnStatusEnum;
import com.ruoyi.system.domain.AppCreateJobBill;
import com.ruoyi.system.domain.AppCreateJobBillV2;
import com.ruoyi.system.domain.bill.AppCreateBill;
import com.ruoyi.system.domain.vo.DeleteVo;
import com.ruoyi.system.domain.vo.JobTypeCountVO;
import com.ruoyi.system.domain.vo.PreventionCheckTaskCountVO;
import com.ruoyi.system.mapper.AppCreateJobBillMapper;
import com.ruoyi.system.service.AppCreateJobBillService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * App创建作业票(AppCreateJobBill)表控制层
 *
 * @since 2022-12-08 15:21:36
 */
@Slf4j
@RestController
@RequestMapping("app/create/job/bill")
public class AppCreateJobBillController extends BaseController {
    @Resource
    private AppCreateJobBillService appCreateJobBillService;
    @Resource
    private AppCreateJobBillMapper appCreateJobBillMapper;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("list")
    @ApiOperation("获取列表")
    public TableDataInfo queryByPage(AppCreateJobBill appCreateJobBill) {
        startPage();
        List<AppCreateJobBill> list = appCreateJobBillService.queryAll(appCreateJobBill);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<AppCreateJobBill> queryById(@PathVariable("id") Integer id) {
        AppCreateJobBill appCreateJobBill = this.appCreateJobBillService.queryById(id);
        log.info("通过主键查询单条数据 结果:{}", JSON.toJSONString(appCreateJobBill));
        return ResponseEntity.ok(appCreateJobBill);
    }

    /**
     * 新增数据
     *
     * @return 新增结果
     */
    @PostMapping("add")
    @ApiOperation("新增")
    public ResponseEntity<AppCreateJobBill> add(@Validated @RequestBody AppCreateJobBill appCreateJobBill) {
        //获取当前登录用户;
        appCreateJobBill.setApply(getLoginUser().getUsername());
        return ResponseEntity.ok(this.appCreateJobBillService.insert(appCreateJobBill));
    }

    @PostMapping("v2/add")
    @ApiOperation("新增")
    public ResponseEntity<AppCreateJobBillV2> addV2(@Validated @RequestBody AppCreateJobBillV2 appCreateJobBillV2) {
        //获取当前登录用户;
        appCreateJobBillV2.setApply(getLoginUser().getUsername());
        return ResponseEntity.ok(this.appCreateJobBillService.insertV2(appCreateJobBillV2));
    }

    /**
     * 编辑数据
     *
     * @return 编辑结果
     */
    @PutMapping("edit")
    @ApiOperation("修改")
    public ResponseEntity<AppCreateJobBill> edit(@Validated @RequestBody AppCreateJobBill appCreateJobBill) {
        log.info("APP签字 编辑数据 ：{}", JSON.toJSONString(appCreateJobBill));
        return ResponseEntity.ok(this.appCreateJobBillService.update(appCreateJobBill));
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    @ApiOperation("删除")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody DeleteVo deleteVo) {
        return ResponseEntity.ok(this.appCreateJobBillService.deleteById(deleteVo));
    }

    @PostMapping("/create/number")
    @ApiOperation("生成单号服务")
    public AjaxResult createNumber(@Validated @RequestBody AppCreateBill appCreateBill) {
        if (null == appCreateBill) {
            return AjaxResult.error("参数异常。");
        }

        //校验审批状态不为空。
        if (null == appCreateBill.getBillType()) {
            return AjaxResult.error("作业票类型不可为空。");
        }

        return AjaxResult.success("");
    }

    @PostMapping("/create/number/test")
    @ApiOperation("生成单号服务")
    public AjaxResult createNumberTest(@Validated @RequestBody AppCreateBill appCreateBill) {
        if (null == appCreateBill) {
            return AjaxResult.error("参数异常。");
        }

        //校验审批状态不为空。
        if (null == appCreateBill.getBillType()) {
            return AjaxResult.error("作业票类型不可为空。");
        }

        String number = appCreateJobBillService.createNumber(appCreateBill);

        return AjaxResult.success(number);
    }

    @PostMapping("/by/ptw")
    @ApiOperation("获取所有票据号")
    public ResponseEntity<List<AppCreateJobBill>> getAllBillNumberByPtw(@Validated @RequestBody AppCreateJobBill appCreateJobBill) {
        List<AppCreateJobBill> appCreateJobBills = appCreateJobBillService.queryAll(appCreateJobBill);
        return ResponseEntity.ok(appCreateJobBills);
    }

    @PostMapping("/mergeJson")
    public String mergeJson(@RequestBody AppCreateJobBill appCreateBill) {
        return appCreateJobBillService.mergeJson(appCreateBill);
    }

    /**
     * 作业数量统计（今日/本月)
     */
    @GetMapping("/countJboNumber")
    public AjaxResult countJboNumber() {
        List<PreventionCheckTaskCountVO> allTask = new ArrayList<>();

        PreventionCheckTaskCountVO allDay = new PreventionCheckTaskCountVO();
        int dayCount = appCreateJobBillService.countCreateJobBill(YnStatusEnum.N.getCode());
        allDay.setName("日");
        allDay.setCount(dayCount);

        Integer monthCount = appCreateJobBillService.countCreateJobBill(YnStatusEnum.Y.getCode());
        PreventionCheckTaskCountVO allMonth = new PreventionCheckTaskCountVO();
        allMonth.setName("月");
        allMonth.setCount(monthCount);

        allTask.add(allDay);
        allTask.add(allMonth);
        return AjaxResult.success(allTask);
    }

    /**
     * 作业分类（日/月/年）（0全部、1今日、2本月、3本年）
     */
    @GetMapping("/countJboTypeNumber")
    public AjaxResult countJboTypeNumber(Integer tempType) {
        List<JobTypeCountVO> allTask = new ArrayList<>();

        if (1 == tempType) {
            // 按日统计
            allTask = appCreateJobBillService.countCreateJobBillTwo(YnStatusEnum.N.getCode());
        } else if (2 == tempType) {
            // 按周统计
            allTask = appCreateJobBillService.countCreateJobBillTwo(tempType);
        } else if (3 == tempType) {
            // 按月统计
            allTask = appCreateJobBillService.countCreateJobBillTwo(YnStatusEnum.Y.getCode());
        } else {
            // 全部
            allTask = appCreateJobBillMapper.getJboTypeNumberAll();
        }

        return AjaxResult.success(allTask);
    }

    @PostMapping("copy")
    @ApiOperation("复制")
    public AjaxResult copy(@Validated @RequestBody AppCreateJobBill appCreateJobBill) {
        if (null == appCreateJobBill || null == appCreateJobBill.getId()) {
            return AjaxResult.error("参数异常");
        }
        AppCreateJobBill createJobBill = appCreateJobBillService.copy(appCreateJobBill);
        return AjaxResult.success(createJobBill);
    }

    @PostMapping("sendMessage")
    @ApiOperation("推送消息")
    public AjaxResult sendMessage(@Validated @RequestBody AppCreateJobBill appCreateJobBill) {
        if (null == appCreateJobBill || null == appCreateJobBill.getId()) {
            return AjaxResult.error("参数异常");
        }
        AppCreateJobBill createJobBill = appCreateJobBillService.queryById(appCreateJobBill.getId());
        appCreateJobBillService.seekNextNode(createJobBill);
        return AjaxResult.success(createJobBill);
    }


}

